function detectSpindles(As, windowsCount)
dim =  windowsCount;
I = 2;
z = 1;
CH = 1;

t = 1:(dim-1);
realtime = 0.0078125*t; % 16/2048  =  0.0078125       

 TIME = [];
 TIMH1 = []; % arxh koryfis
 TIMH2 = []; % telos koryfis
 TIMH1_1 = [];
 TIMH2_2 = [];
 DIARKEIA = []; % xroniko diasthma koryfis
 KORYFI = []; % shmata pou einai koryfes
 TAG = [];
 DIARKEIA_TOTAL = [];
 TIMH1_HARD = [];
 DIARKEIA_TOTAL_HARD = [];
 SOFT_ISIs = [];
 HARD_ISIs = [];
 HARD = [];
 DURANCE_HARD = [];
 
 As = As';
 TIME = realtime;
 TIME = TIME';
 J = 1;
 K = 1;
 L = 1;
 M = 1;
 X = 1;
 N = 1;

 DIM = length(As);
  
 % kritiria
		
 % kritirio C1
 while I<(DIM)
	while  (I <= (DIM)) && (As(I)<= 0.9)
		I = I+1;
	end
 
	if I ~= (DIM+1)
		START_POINT = TIME(I);
		TIMH1(K) = START_POINT;
		K = K+1;
 
		while (I <= (DIM)) && (As(I + 1) > 0.9)
			KORYFI(J) = As(I);
			J = J+1;
			I = I+1;
		end
 
		KORYFI(J) = As(I);
		J = J+1;
		END_POINT = TIME(I);
		TIMH2(L) = END_POINT;
		L = L+1;
		DIARKEIA(M) = END_POINT - START_POINT;
		M = M+1;
		I = I+1;
	end
 end
	
 % synenwsi koryfwn
 NUM_KORIFON = length(DIARKEIA);
 if NUM_KORIFON ~= 0
	for N = 1:(M-2)
		TAG(N) = TIMH1(N+1)-TIMH2(N);
	end
 
	DIM3 = length(TAG);
	DIM1 = length(TIME);   

	for I = 1:DIM3
		if TAG(I)>0.3
			TAG(I) = TAG(I);
		elseif TAG(I)<0.3
			for K = 1:(DIM1-1)
				if TIME(K) == TIMH2(I)
					START = K;
				end 
			end

			for K = 1:(DIM1-1)
				if TIME(K) == TIMH1(I+1)
					END = K;
				end
			end

			for J = START:END
				if As(J+1)<0.8
					TAG(I) = TAG(I);
				elseif As(J+1)>0.8
					TAG(I) = 0;
				end
			end
		end		
	end

	M = 1;
	for I = 1:DIM3
		if TAG(I)~= 0
			TIMH1(M) = TIMH1(M);
			TIMH2(M) = TIMH2(M);
			M = M+1;
		else
			TIMH1(M) = TIMH1(M);
			TIMH1(M+1) = 0;
			TIMH2(M) = TIMH2(M+1);
			TIMH2(M+1) = 0;
			M = M+1;
		end
	end

	V = length(TIMH1);
	R = 1;
	S = 1;
	while R<= V
		if (TIMH1(R) == 0) && (TIMH2(R) == 0)
			R = R+1;
		else
			TIMH1_1(S) = TIMH1(R);
			TIMH2_2(S) = TIMH2(R);
			R = R+1;
			S = S+1;
		end
	end	
	
	% ypologismos xronikis diarkeias kathe atraktou
	for I = 1:(length(TIMH1_1))
		DIARKEIA_TOTAL(I) = TIMH2_2(I)-TIMH1_1(I);
	end
 else
	out_filname  =  'none.dat';
	fid = fopen(out_filname,'w');
	fprintf(fid,'kamia atraktos');
	fclose(fid);              
 end
 
 % kritirio C2
 NUM_KORIFON = length(DIARKEIA);

 if NUM_KORIFON ~= 0
	dimension = length(DIARKEIA_TOTAL);
	P = 1;
	
	while P <= dimension
		if DIARKEIA_TOTAL(P)>0.3
			DIARKEIA_TOTAL_HARD(P) = DIARKEIA_TOTAL(P);
			TIMH1_HARD(P) = TIMH1_1(P);
			P = P+1;
		else
			DIARKEIA_TOTAL_HARD(P) = 0;
			TIMH1_HARD(P) = 0;
			P = P+1;
		end
	end
 
	K = 1;
 
	% diarkeia tou ISI
	for I = 1:(length(TIMH1_1)-1)
		SOFT_ISIs(K) = TIMH1_1(I+1)-TIMH1_1(I);
		K = K+1;
	end


	% bubble sort (?!!)                           
	for I = 2:length(TIMH1_HARD)
		J = length(TIMH1_HARD);
		while J >= I
			if TIMH1_HARD(J-1)>TIMH1_HARD(J)
				temp = TIMH1_HARD(J-1);
				TIMH1_HARD(J-1) = TIMH1_HARD(J);
				TIMH1_HARD(J) = temp;
				temp1 = DIARKEIA_TOTAL_HARD(J-1);
				DIARKEIA_TOTAL_HARD(J-1) = DIARKEIA_TOTAL_HARD(J);
				DIARKEIA_TOTAL_HARD(J) = temp1;
			end
			J = J-1;
		end                                           
	end

	d = length(TIMH1_HARD);
	a = 1;
	b = 1;

	while a <= d
		if (TIMH1_HARD(a) == 0) && (DIARKEIA_TOTAL_HARD(a) == 0)
			a = a+1;
		else
			HARD(b) = TIMH1_HARD(a);
			DURANCE_HARD(b) = DIARKEIA_TOTAL_HARD(a);
			b = b+1;
			a = a+1;
		end
	end

	c = 1;
	for d = 1:(length(HARD)-1)
		HARD_ISIs(c) = HARD(d+1)-HARD(d);
		c = c+1;
	end
 end

 %RESULTS OF SPINLDES DETECTION WITHOUT TEXT

 % results of the C2 soft criterio 
 TIMH1_1 = TIMH1_1';
 DIARKEIA_TOTAL = DIARKEIA_TOTAL';
 RESULTS_SOFT = [TIMH1_1 DIARKEIA_TOTAL];
 RESULTS_SOFT = RESULTS_SOFT';
 out_filname  =  ['RESULTS_SOFT_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid,' %5.2f %10.2f\n', RESULTS_SOFT);
 fclose(fid);

 % results of the C2 hard criterio
 HARD = HARD';
 DURANCE_HARD = DURANCE_HARD';
 RESULTS_HARD = [HARD DURANCE_HARD];
 RESULTS_HARD = RESULTS_HARD';
 out_filname  =  ['RESULTS_HARD_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid,'%5.2f %10.2f\n',RESULTS_HARD);
 fclose(fid);

 % results of the ISIs soft criterio
 out_filname  =  ['SOFT_ISIs_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid,'%5.2f\n',SOFT_ISIs);
 fclose(fid);

 % results of the ISIs hard criterio
 out_filname  =  ['HARD_ISIs_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid,'%5.2f\n',HARD_ISIs);
 fclose(fid);


 %RESULTS OF SPINLDES DETECTION WITH TEXT_NAMES

 % results of the C2 soft criterio
 RESULTS_SOFT = [TIMH1_1 DIARKEIA_TOTAL];
 NUMBER = 1:length(TIMH1_1);
 NUMBER = NUMBER';
 RESULTS_SOFT = [RESULTS_SOFT NUMBER];
 RESULTS_SOFT = RESULTS_SOFT';
 out_filname  =  ['TEXT_RESULTS_SOFT_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid, ' %60s\n\n', 'SOFT CRITERION APPLIED FOR SPINDLE DETECTION');
 fprintf(fid,' %20s %20s %20s\n', 'SPINDLE OCCURENCE TIME','SPINDLE DURATION','SPINDLE NUMBER');
 fprintf(fid,'%15.2f %20.2f %20.0f\n', RESULTS_SOFT);
 fclose(fid);

 % results of the C2 hard criterio
 RESULTS_HARD = [HARD DURANCE_HARD];
 NUMBER_1 = 1:length(HARD);
 NUMBER_1 = NUMBER_1';
 RESULTS_HARD = [RESULTS_HARD NUMBER_1];
 RESULTS_HARD = RESULTS_HARD';
 out_filname  =  ['TEXT_RESULTS_HARD_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid, '%60s\n\n', 'HARD CRITERION APPLIED FOR SPINDLE DETECTION');
 fprintf(fid,' %20s %20s %20s\n', 'SPINDLE OCCURENCE TIME','SPINDLE DURATION','SPINDLE NUMBER');
 fprintf(fid,'%15.2f %20.2f %20.0f\n',RESULTS_HARD);
 fclose(fid);

 % results of the ISIs soft criterio
 NUMBER_2 = 1:length(SOFT_ISIs);
 SOFT_ISIs = [SOFT_ISIs NUMBER_2];
 out_filname  =  ['TEXT_SOFT_ISIs_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid,' %20s %20s\n', 'ISI DURATION','ISI NUMBER');
 fprintf(fid,'%15.2f %20.0f\n',SOFT_ISIs);
 fclose(fid);


 % results of the ISIs hard criterio
 NUMBER_3 = 1:length(HARD_ISIs);
 HARD_ISIs = [HARD_ISIs NUMBER_3];
 out_filname  =  ['TEXT_HARD_ISIs_CH',int2str(CH(z)),'.doc'];
 fid = fopen(out_filname,'w');
 fprintf(fid,' %20s %20s\n', 'ISI DURATION','ISI NUMBER');
 fprintf(fid,'%15.2f %20.0f\n',HARD_ISIs);
 fclose(fid);